//
//  NYMyAirlineViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyAirlineViewController.h"
#import "NYMyAirlineRequest.h"
#import "NYRefreshFooter.h"
@interface NYMyAirlineViewController ()
@property(nonatomic, strong) NYMyAirlineRequest *request;
@end

@implementation NYMyAirlineViewController


#pragma mark - Life Cycle Methods

- (void)viewDidLoad
{
        [super viewDidLoad];
        
        [self loadData];
}

- (void)loadData {
        
        [self setLoading:YES animation:YES];
        [self configNimbus];
        self.request = [NYMyAirlineRequest requestWithParams:nil];
        
        @weakify(self);
        [[self.request pullDown] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYAirlineDataModel * _Nonnull data) {
                @strongify(self);
                [self addNimbusData:data];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                @strongify(self);
                [self setError:error];
        }];
}
- (void)setSuccess {
        [super setSuccess];
        [self configRefreshFooter];
}
- (void)addNimbusData:(NYAirlineDataModel * _Nonnull )data
{
        /// 加载用户头像
        [data.list enumerateObjectsUsingBlock:^(NYAirlineItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (NULLString(obj.problem_avatar)) {
                        obj.problem_avatar = NYUserManager.avatar;
                }
                if (NULLString(obj.problem_username)) {
                        obj.problem_username = NYUserManager.nickname;
                }
        }];
        [self.tableViewModel addObjectsFromArray:data.list];
        [self.tableView reloadData];
}
- (void)configRefreshFooter {
        
        @weakify(self);
        self.tableView.mj_footer = [NYRefreshFooter footerWithRefreshingBlock:^{
                @strongify(self);
                [[self.request pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYAirlineDataModel * _Nonnull data) {
                        [self addNimbusData:data];
                        [data.list count] == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }];
        }];
}

- (void)configNavigationBar:(BaseNavigationBar *)navigationBar {
        [super configNavigationBar:navigationBar];
        self.title = @"我的疑问";
}
+ (NYViewControllerNavigationBarType)navigationBarType {
        
        return NYViewControllerNavigationBarTypeBlue;
}
@end
